package zmee.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yss
 * @createTime 2022-12-26 16:54
 * @description
 */
public class DateUtil {

    /**
     * 获得当日0点
     */
    public static Date getZero(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    /**
     * 获得当日最后时间
     */
    public static Date getEnd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当年最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTimeOfYear(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, 0);
        return getFirstTimeOfMonth(c.getTime());
    }

    /**
     * 获取当年最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTimeOfYear(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, 11);
        return getEndTimeOfMonth(c.getTime());
    }

    /**
     * 获取当月最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTimeOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return getFirstTime(c.getTime());
    }

    /**
     * 获取当月最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTimeOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return getEndTime(c.getTime());
    }

    /**
     * 获取当天最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取当天最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        if (date == null) {
            return null;
        }
        Date dateFirstTime = getFirstTime(date);
        Calendar c = Calendar.getInstance();
        c.setTime(dateFirstTime);
        c.add(Calendar.DATE, 1);
        c.add(Calendar.MILLISECOND, -1);
        return c.getTime();
    }

    public static Date getDateByYear(Integer year){
        //获取当年最开始时间和最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取指定年月的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonthByDate(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String periodTime = format.format(date);
        String[] periodTimeArray = periodTime.split("-");
        return getFirstDayOfMonth(Integer.parseInt(periodTimeArray[0]),Integer.parseInt(periodTimeArray[1]));
    }

    /**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year,Integer month){
        Calendar instance = Calendar.getInstance();
        //设置年份
        instance.set(Calendar.YEAR,year);
        //设置月份
        instance.set(Calendar.MONTH,month - 1);
        //获取某月最小天数
        int maximum = instance.getMaximum(Calendar.DATE);
        //设置日历中月份最小天数
        instance.set(Calendar.DAY_OF_MONTH,maximum);
        //格式化日期
        return instance.getTime();
    }

    /**
     * 获取指定年月的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfMonthByDate(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String periodTime = format.format(date);
        String[] periodTimeArray = periodTime.split("-");
        return getLastDayOfMonth(Integer.parseInt(periodTimeArray[0]),Integer.parseInt(periodTimeArray[1]));
    }

    /**
     * 获取指定年月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year,Integer month){
        Calendar instance = Calendar.getInstance();
        //设置年份
        instance.set(Calendar.YEAR,year);
        //设置月份
        instance.set(Calendar.MONTH,month - 1);
        //获取某月最小天数
        int maximum = instance.getActualMaximum(Calendar.DATE);
        //设置日历中月份最小天数
        instance.set(Calendar.DAY_OF_MONTH,maximum);
        //格式化日期
        return instance.getTime();
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth(int tear,int march) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(tear, march, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth(int tear,int march ) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(tear, march, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(tear, march, day);
        return getDayEndTime(calendar.getTime());
    }

    // 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取本年的结束时间
    public static Date getEndDayOfYear(int yuar) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yuar);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }
    //本年开始时间
    public static Date getBeginDayOfYear(int yuar) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yuar);
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 将指定日期时间年份(date)加上指定数(time)
     * @param date
     * @param time
     * @return
     */
    public static Date yearAdd(Date date, int time) {
        // 创建Calendar实例
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 在当前时间基础上加上time
        calendar.add(Calendar.YEAR, time);

        return calendar.getTime();
    }

    /**
     * 将指定日期转换为现在普遍的时间格式以字符串格式(String)输出
     * @param date
     * @return
     */
    public static String getCommonFormat(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }
}
